View Javadoc

1   // Win32FileSystem.java, created Thu Jul  4  4:50:03 2002 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.ClassLib.Common.java.io;
5   
6   import joeq.Memory.Address;
7   import joeq.Memory.HeapAddress;
8   import joeq.Runtime.SystemInterface;
9   
10  /***
11   * Win32FileSystem
12   *
13   * @author  John Whaley <jwhaley@alum.mit.edu>
14   * @version $Id: Win32FileSystem.java 1456 2004-03-09 22:01:46Z jwhaley $
15   */
16  public abstract class Win32FileSystem {
17  
18      public java.lang.String canonicalize(java.lang.String s) throws java.io.IOException {
19          // check for and eliminate wildcards.
20          if ((s.indexOf('*')>=0) || (s.indexOf('?')>=0))
21              throw new java.io.IOException("wildcards not allowed in file name: "+s);
22          byte[] b = new byte[256];
23          int r = SystemInterface.fs_fullpath(s, b);
24          if (r == 0) throw new java.io.IOException("fullpath returned error on: "+s);
25          java.lang.String res = SystemInterface.fromCString(HeapAddress.addressOf(b));
26          int strlen = res.length();
27          java.lang.StringBuffer result = new java.lang.StringBuffer(strlen);
28          int curindex = 0;
29          if (res.startsWith("////")) {
30              // trim trailing "\" on UNC name.
31              //if (res.charAt(strlen-1) == '//') { res = res.substring(0, strlen-1); --strlen; }
32              curindex = res.indexOf('//', 2);
33              if (curindex == -1) throw new java.io.IOException("invalid UNC pathname: "+s);
34              result.append(res.substring(0, curindex));
35          } else if (res.charAt(1) == ':') {
36              // change drive letter to upper case.
37              result.append(Character.toUpperCase(res.charAt(0)));
38              result.append(':');
39              curindex = 2;
40          }
41          while (curindex < strlen) {
42              result.append('//');
43              int next_idx = res.indexOf('//', curindex);
44              if (next_idx == -1) {
45                  result.append(res.substring(curindex));
46                  return result.toString();
47              }
48              java.lang.String sub = res.substring(curindex, next_idx);
49              Address b3 = SystemInterface.fs_gettruename(sub);
50              if (b3.isNull()) {
51                  // bail out and return what we have.
52                  result.append(res.substring(curindex));
53                  return result.toString();
54              }
55              result.append(SystemInterface.fromCString(b3));
56              curindex = next_idx+1;
57          }
58          // path name ended in "\"
59          return result.toString();
60      }
61      
62      /* Constants for simple boolean attributes */
63      public static final int BA_EXISTS    = 0x01;
64      public static final int BA_REGULAR   = 0x02;
65      public static final int BA_DIRECTORY = 0x04;
66      public static final int BA_HIDDEN    = 0x08;
67      public int getBooleanAttributes(java.io.File file) {
68          int res = SystemInterface.fs_getfileattributes(file.getPath());
69          if (res == -1) return 0;
70          return BA_EXISTS |
71                 (((res & SystemInterface.FILE_ATTRIBUTE_DIRECTORY) != 0)?BA_DIRECTORY:BA_REGULAR) |
72                 (((res & SystemInterface.FILE_ATTRIBUTE_HIDDEN) != 0)?BA_HIDDEN:0);
73      }
74  
75      public boolean checkAccess(java.io.File file, boolean flag) {
76          int res = SystemInterface.fs_access(file.getPath(), flag?2:4);
77          return res == 0;
78      }
79  
80      public long getLastModifiedTime(java.io.File file) {
81          long res = SystemInterface.fs_getfiletime(file.getPath());
82          return res;
83      }
84  
85      public long getLength(java.io.File file) {
86          return SystemInterface.fs_stat_size(file.getPath());
87      }
88      
89      public boolean delete(java.io.File file) {
90          int res = SystemInterface.fs_remove(file.getPath());
91          return res == 0;
92      }
93      
94      public String[] list(java.io.File file) {
95          int dir = SystemInterface.fs_opendir(file.getPath());
96          if (dir == 0) return null;
97          String[] s = new String[16];
98          int i;
99          Address ptr;
100         for (i=0; !(ptr=SystemInterface.fs_readdir(dir)).isNull(); ++i) {
101             if (i == s.length) {
102                 String[] s2 = new String[s.length<<1];
103                 System.arraycopy(s, 0, s2, 0, s.length);
104                 s = s2;
105             }
106             s[i] = SystemInterface.fromCString(ptr.offset(SystemInterface.readdir_name_offset));
107             if (s[i].equals(".") || s[i].equals("..")) --i;
108         }
109         SystemInterface.fs_closedir(dir);
110         String[] ret = new String[i];
111         System.arraycopy(s, 0, ret, 0, i);
112         return ret;
113     }
114 
115     public boolean createDirectory(java.io.File file) {
116         int res = SystemInterface.fs_mkdir(file.getPath());
117         return res == 0;
118     }
119 
120     public boolean rename(java.io.File file, java.io.File file1) {
121         int res = SystemInterface.fs_rename(file.getPath(), file1.getPath());
122         return res == 0;
123     }
124 
125     public boolean setLastModifiedTime(java.io.File file, long l) {
126         int res = SystemInterface.fs_setfiletime(file.getPath(), l);
127         return res != 0;
128     }
129 
130     public boolean setReadOnly(java.io.File file) {
131         int res = SystemInterface.fs_chmod(file.getPath(), SystemInterface._S_IREAD);
132         return res == 0;
133     }
134 
135     private static int listRoots0() {
136         return SystemInterface.fs_getlogicaldrives();
137     }
138     
139     private static void initIDs() { }
140     
141     //public static final jq_Class _class = (jq_Class)PrimordialClassLoader.loader.getOrCreateBSType("Ljava/io/Win32FileSystem;");
142     //public static final jq_Initializer _constructor = (jq_Initializer)_class.getOrCreateInstanceMethod("<init>", "()V");
143 }